//set working directory 
cd  "C:\Users\johna\Dropbox (Curiel Analytx)\ElectionNightResults\replication_code"
//the above is just the path to the replcation files. The data from here on are 
//hierarchical and nested from this file path. 

//read in the state dta file 
use "data\nyt_states.dta", clear

drop total // Don't need this.  We'll generate two-party total later


** calculate reporting momentum

gen total4 = norm_total if hoursfromclose <= 4 & hoursfromclose[_n+1] > 4
gen total8 = norm_total if hoursfromclose <= 8 & hoursfromclose[_n+1] > 8
gen total24 = norm_total if hoursfromclose <= 24 & hoursfromclose[_n+1] > 24
gen total48 = norm_total if hoursfromclose <= 48 & hoursfromclose[_n+1] > 48

egen maxtotal4 = max(total4),by(state)
egen maxtotal8 = max(total8),by(state)
egen maxtotal24 = max(total24),by(state)
egen maxtotal48 = max(total48),by(state)


** calculate variance from eventual vote margin

gen dempct4 = norm_dempct if hoursfromclose <= 4 & hoursfromclose[_n+1] > 4
gen dempct8 = norm_dempct if hoursfromclose <= 8 & hoursfromclose[_n+1] > 8
gen dempct24 = norm_dempct if hoursfromclose <= 24 & hoursfromclose[_n+1] > 24
gen dempct48 = norm_dempct if hoursfromclose <= 48 & hoursfromclose[_n+1] > 48

egen maxdempct4 = max(dempct4),by(state)
egen maxdempct8 = max(dempct8),by(state)
egen maxdempct24 = max(dempct24),by(state)
egen maxdempct48 = max(dempct48),by(state)


keep if state ~= state[_n+1]
replace maxdempct4 = 1.308403 if state == "hawaii"

gsort -maxdempct4
gen order1 = _n
egen maxrange = rowmax(maxdempct*)
egen minrange = rowmin(maxdempct*)
//gen zero = 0
gen minus10 = -11

gen state_lbl = proper(subinstr(state,"-"," ",.))
replace state_lbl = "D.C." if state_lbl == "District Of Columbia"

drop state
rename state_lbl state
merge 1:1 state using "data\state_codes", nogen
drop state
rename state_po state
replace state = "DC" in 8
//drop in 52

maptile maxdempct4,geo(statehex) cutvalues(-.5 .5) rangecolor(red blue) twopt( title(After 4 hours)  legend(order(4 "0.5 - 4.2" 3 "-0.5 - 0.5" 2 "-10.2 - -0.5") title("Comparison to final" "Biden pct.",size(*.7))) )
graph export "figures\fig6a.png", replace width(2500)

maptile maxdempct8,geo(statehex) cutvalues(-.5 .5) rangecolor(red blue) twopt( title(After 8 hours)  legend(order(4 "0.5 - 4.2" 3 "-0.5 - 0.5" 2 "-10.2 - -0.5") title("Comparison to final" "Biden pct.",size(*.7))) )
graph export "figures\fig6b.png", replace width(2500)

maptile maxdempct24,geo(statehex) cutvalues(-.5 .5) rangecolor(red blue) twopt( title(After 24 hours)  legend(order(4 "0.5 - 4.2" 3 "-0.5 - 0.5" 2 "-10.2 - -0.5") title("Comparison to final" "Biden pct.",size(*.7))) )
graph export "figures\fig6c.png", replace width(2500)

maptile maxdempct48,geo(statehex) cutvalues(-.5 .5) rangecolor(red blue) twopt( title(After 48 hours)  legend(order(4 "0.5 - 4.2" 3 "-0.5 - 0.5" 2 "-10.2 - -0.5") title("Comparison to final" "Biden pct.",size(*.7))) )
graph export "figures\fig6d.png", replace width(2500)



//save $dir:\scratch\variance, replace
